package com.agile.leetcode.lru;

import java.util.LinkedList;

/**
 * @Author:ChenZhangKun
 * @Date: 2023/7/7 12:07
 */
public class LinkedListLru<E> {
    private int max;
    private LinkedList<E> linkedList;

    public LinkedListLru(int max) {
        this.max = max;
    }

    public void add(E e){
        if (linkedList.size()<max){
            linkedList.addFirst(e);
        }else {
            // 移除
            linkedList.removeLast();
            linkedList.addFirst(e);
        }
    }
    public E get(int index){
        E e = linkedList.get(index);
        linkedList.remove(e);
        add(e);
        return e;
    }
}
